package com.asset.salesforce.dao; 

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.ui.Model;

import com.asset.salesforce.domain.Employee;
import com.asset.salesforce.domain.JoinRequest;
import com.asset.salesforce.service.JoinRequestService;

@Repository
public class JoinRequestDaoImpl {
	
	@Autowired
	SessionFactory sessionFactory;
	
	@Autowired
	private JoinRequestService joinRequestService;
	
	public void saveOrupdate(JoinRequest joinRequest){
		Session session = sessionFactory.getCurrentSession();
		session.saveOrUpdate(joinRequest);
	}
	
	public void addJoinRequest(JoinRequest joinRequest) {
		try{
		sessionFactory.getCurrentSession().save(joinRequest);
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	public List<JoinRequest> getJoinRequestByOwnerId(Integer ownerId){
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("from JoinRequest where  owner.id = :ownerId and status= :status");
		query.setParameter("ownerId", ownerId);
		query.setParameter("status", 0);
		@SuppressWarnings("unchecked")
		List<JoinRequest>  joinRequests = query.list();
		if(joinRequests != null && joinRequests.size() > 0)
		return joinRequests;
		else
			return new ArrayList<JoinRequest>();
		
	}
	public JoinRequest getJoinRequestByEmpId(Integer Emp){
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("from JoinRequest where  emp.id = :Emp and status= :status");
		query.setParameter("Emp", Emp);
		query.setParameter("status", 2);
		@SuppressWarnings("unchecked")
		List<JoinRequest>  joinRequests = query.list();
		if(joinRequests != null && joinRequests.size() > 0)
		return joinRequests.get(0);
		else
			return null;		
	}
	
	public JoinRequest getJoinRequest(Integer id) {
		Session session = sessionFactory.getCurrentSession();
		return (JoinRequest)session.get(JoinRequest.class, id);
		
	}
	
	public void deleteJoinRequest(Integer id) {
		JoinRequest joinRequest = (JoinRequest) sessionFactory.getCurrentSession().load(
				JoinRequest.class, id);
		if (null != joinRequest) {
			sessionFactory.getCurrentSession().delete(joinRequest);
		}
		
	}
	public void getJoinRquestNo(HttpSession session, Model model ) {
		Integer joinRequestCount = 0;
		if(null !=session.getAttribute("userDetails") ){
			Employee employee = (Employee)session.getAttribute("userDetails");
			List<JoinRequest> joinRequests  = joinRequestService.getJoinRequestsByOwnerId(employee.getId());
			joinRequestCount = joinRequests.size();
		}
		model.addAttribute("joinRequestCount",joinRequestCount);
	}
	public void getJoinRquestNum(Employee employee) {
		Integer joinRequestCount = 0;
		List<JoinRequest> joinRequests  = joinRequestService.getJoinRequestsByOwnerId(employee.getId());
		joinRequestCount = joinRequests.size();
	}
}
